<?php
// +----------------------------------------------------------------------
// | Author: Stupboy King
// +----------------------------------------------------------------------
namespace app\api\model;
use think\Db;
use think\exception\DbException;

class Passenger extends Common
{
    public function user(){
        return $this->hasOne('User','user_id','user_id');
    }

    /**
     * 获取司机登入信息
     * @param $user
     * @param $pr_where
     * @return array|false|\PDOStatement|string|\think\Model
     */
    public function getPassengerInfo($user,$pr_where){
        $field = 'a.passenger_id,head_portrait AS passenger_head_portrait,a.name AS passenger_name,a.phone AS passenger_phone,unit AS passenger_unit,is_check,is_instead,b.company_name passenger_unit,b.prop,a.is_vip,a.company_id';
        $info  = $this->alias('a')
            ->join('company b','a.company_id = b.company_id')
            ->where($pr_where)->field($field)->find();
        if(empty($info)){
            abort(0,'乘客不存在');
        }
        $info['user_id']    = $user['user_id'];
        $info['is_set_pwd'] = $user['is_set_pwd'];
        $info['prop']       = explode(',',$info['prop']);
        $info['prop']       = in_array(13,$info['prop']) ? 13 : 0;
        $info['prop']       = $info['is_vip']== 1 && $info['prop'] == 13 ? 13 : 0;
        $info['role_id']    = $info['is_check']==1 ? 3 : 0;//是否有审核员权限判断字段
        $passenger_head_portrait = $info['passenger_head_portrait'];
        $arr      = explode('/',$passenger_head_portrait);
        $img_name = $arr[count($arr)-1];
        $img_file = $arr[count($arr)-2];
        $info['passenger_head_portrait'] = "new.jjcxfw.com/upload/head_portrait/". $img_file ."/".$img_name;
        $info['head_url'] = 'http://'.$info['passenger_head_portrait'];
        return $info;
    }

    /**
     * 乘客订单支付
     * @param $oid
     * @param $pid
     * @param int $money
     * @param int $payType
     * @return mixed
     */
    public function orderPay($oid,$pid,$money=0,$payType=1){
        //TODO 增加使用额度 增加流水
        $payType = intval($payType) <1 || intval($payType) >4 ? 1 :  intval($payType);
        $payWay  = ['personalPay','companyPay','financePay','selfPay'];
        $payType = $payWay[$payType - 1];
        return $this->$payType($oid,$pid,$money);
    }

    /**
     * @param $oid
     * @param $pid
     * @param $money
     * @return array
     * @throws \think\Exception
     */
    public function selfPay($oid,$pid,$money){
        // TODO 更新订单费用表乘客费用字段
        try{
            Db::name('order')->update(['order_id'=>$oid,'order_state'=>10,'payment_time'=>date('Y-m-d H:i:s')]);
            return [1,'ok'];
        }catch (DbException $e){
            return [0,'支付异常'];
        }
    }

    /**
     * 财政支付
     * @param $oid
     * @param $pid
     * @param $money
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function financePay($oid,$pid,$money){
        $carUse = new Company();
        $quota  = $carUse->getUseMoneyByPassenger($pid);
        $left   = $quota['quota'] - $quota['office_money'];
        if($money<=$left){
            Db::name('order')->update(['order_id'=>$oid,'order_state'=>10,'payment_time'=>date('Y-m-d H:i:s')]);
            //更新额度
            Db::name('company')->where('company_id',$quota['company_id'])->update(['used_quota'=>['exp',"used_quota + $money"]]);
            return [1,'ok'];
        }else{
            return [0,0];
        }
    }

    /**
     * 单位支付
     * @param $oid
     * @param $pid
     * @param $money
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function companyPay($oid,$pid,$money){
        $carUse = new Company();
        $quota  = $carUse->getUseMoneyByPassenger($pid);
        $left   = $quota['unit_quota'] - $quota['company_money'];
        if($money<=$left){
            //更新订单状态
            Db::name('order')->update(['order_id'=>$oid,'order_state'=>10,'payment_time'=>date('Y-m-d H:i:s')]);
            //更新额度
            Db::name('company')->where('company_id',$quota['company_id'])->update(['used_unit_quota'=>['exp',"used_unit_quota + $money"]]);
            return [1,'ok'];
        }else{
            return [0,$money];
        }
    }

    /**
     * 个人支付
     * @param $oid
     * @param $pid
     * @param $money
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function personalPay($oid,$pid,$money){
        $field = 'b.balance';
        $info  = $this->alias('a')
            ->join('passenger_account b','a.passenger_id = b.passenger_id','left')
            ->where('a.passenger_id',$pid)
            ->field($field)
            ->find();
        if($money<=$info['balance']){
            Db::name('order')->update(['order_id'=>$oid,'order_state'=>10,'payment_time'=>date('Y-m-d H:i:s')]);
            //更新额度
            Db::name('passenger_account')->where('passenger_id',$pid)->update(['balance'=>['exp',"balance - $money"]]);
            return [1,'ok'];
        }else{
            return [0,$money];
        }
    }

    /**
     * 获取乘车人列表
     * @param $pid
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getPassengerList($pid, $str = '', $cid = 0 ){
        if(!empty($str)){
            $passengers = Db::name('passenger')
                ->where('company_id',$cid)
                ->where("name like '%$str%' or phone = '$str'")
                ->field('passenger_id,name,phone passenger_phone')
                ->select();
            return $passengers;
        }
        $car_passenger_info = Db::name('passenger')->alias('pr')
            ->join('company cu', 'pr.company_id = cu.company_id')
            ->field('pr.company_id,cu.company_name')
            ->where('pr.passenger_id', $pid)->find();
        $department_info = Db::name('department')->where('company_id', $car_passenger_info['company_id'])->field('department_id,name')->select();
        foreach ($department_info as $k => $val) {
            $department_where['department_id'] = $val['department_id'];
            $passenger_info = Db::name('passenger')->where($department_where)->whereNull('delete_time')->field('name AS passenger_name,phone AS passenger_phone')->select();
            $department_info[$k]['passenger_info'] = $passenger_info;
        }
        $info = array(
            'car_use_name'    => $car_passenger_info['company_name'],
            'department_info' => $department_info
        );
        return $info;
    }

    /**
     * @param int $uid
     * @param $old_password
     * @param $new_password
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function modifyPassword($uid=0,$old_password,$new_password){
        if($old_password==$new_password){
            return [0,'新密码与旧密码一致,无需修改'];
        }
        $user = Db::name('user')->where('user_id',$uid)->find();
        if($user['password']==CorePwd($old_password,$user['salt'])){
            $data['password'] = CorePwd($new_password,$user['salt']);
            $ret = Db::name('user')->where('user_id',$uid)->update($data);
            if($ret==1){
                return [$ret,'ok'];
            }else{
                return [$ret,'修改失败'];
            }
        }else{
            return [0,'输入旧密码错误'];
        }
    }
}
